<?php
/**
 * Created by PhpStorm.
 * User: xutao
 * Date: 2018/8/14
 * Time: 15:28
 */
namespace app\index\controller;
use app\common\model\CollectionDestinationModel;
use app\common\model\CollectionSpotModel;
use app\common\model\CollectionTravelsModel;
use app\common\model\TravelsModel;
use think\Db;
use think\Session;

class Cont extends Base{
    //景点收藏
    public function c_stop(){
        if(request()->isPost()){
            if (!Session::get('member')) return json(['code' => 9, 'data' => '', 'msg' => '请登录']);
                $spot = trim(input('spot'));
            if(!$spot) return json(['code'=>1,'data'=>'','msg'=>'参数为空']);
            //查询是否收藏
            $member_id = Session::get('member')['id'];
            $is_cunzai = CollectionSpotModel::where('member_id',$member_id)->where('spot',$spot)->find();
            if($is_cunzai) return json(['code'=>2,'data'=>'','msg'=>'已收藏']);
            $re = CollectionSpotModel::create([
                'member_id'=>$member_id,
                'spot' => $spot
            ]);
            if($re){
                return json(['code'=>200,'data'=>'','msg'=>'收藏成功']);
            }else{
                return json(['code'=>3,'data'=>'','msg'=>'收藏失败']);
            }
        }else{
            return json(['code'=>0,'data'=>'','msg'=>'非法请求']);
        }
    }
    //移除景点收藏
    public function rm_spot(){
        if(request()->isPost()){
            if (!Session::get('member')) return json(['code' => 9, 'data' => '', 'msg' => '请登录']);
            $id = input('id');
            if(!$id) return json(['code'=>1,'data'=>'','msg'=>'参数为空']);
            //查询是否收藏
            $member_id = Session::get('member')['id'];
                $re = CollectionSpotModel::where('member_id',$member_id)->where('id',$id)->delete();
                if($re){
                    return json(['code'=>200,'data'=>'','msg'=>'移除成功']);
                }else{
                    return json(['code'=>0,'data'=>'','msg'=>'移除失败']);
                }
        }else{
            return json(['code'=>0,'data'=>'','msg'=>'非法请求']);
        }
    }
    //获取收藏景点的列表
    public function get_spot(){
        if(request()->isPost()){
            if (!Session::get('member')) return json(['code' => 9, 'data' => '', 'msg' => '请登录']);
            $member_id = Session::get('member')['id'];
            $page = input('page')?input('page'):1;
            $start = $page == 1?0:($page-1)*10;
            //查询数据
            $count = CollectionSpotModel::where('member_id',$member_id)->count();
            $data = CollectionSpotModel::where('member_id',$member_id)->order('create_time desc')->limit($start,10)->select();
            return json(['code'=>200,'data'=>$data,'count'=>$count,'msg'=>'ok']);
        }else{
            return json(['code'=>0,'data'=>'','msg'=>'非法请求']);
        }
    }
    //目的地收藏
    public function c_destination(){
        if(request()->isPost()){
            if (!Session::get('member')) return json(['code' => 9, 'data' => '', 'msg' => '请登录']);
            $member_id = Session::get('member')['id'];
            $country = trim(input('country'));
            $province = trim(input('province'));
            $city = trim(input('city'));
            if(!$country || !$city) return json(['code'=>1,'data'=>'','msg'=>'参数错误']);
            //查询是否已收藏
            $is_cunzai = CollectionDestinationModel::where('member_id',$member_id)->where('country',$country)->where('province',$province)->where('city',$city)->find();
            if($is_cunzai) return json(['code'=>2,'data'=>'','msg'=>'该目的地已收藏']);
            //写入数据库
           $re = CollectionDestinationModel::create([
                'member_id'=>$member_id,
                'country'=>$country,
                'province'=>$province,
                'city'=>$city
            ]);
           if($re){
                return json(['code'=>200,'data'=>'','msg'=>'收藏成功']);
           }else{
               return json(['code'=>3,'data'=>'','msg'=>'收藏失败']);
           }
        }else{
            return json(['code'=>0,'data'=>'','msg'=>'非法请求']);
        }
    }
    //目的地移除
    public function rm_destination(){
        if(request()->isPost()){
            if (!Session::get('member')) return json(['code' => 9, 'data' => '', 'msg' => '请登录']);
            $member_id = Session::get('member')['id'];
            $id = input('id');
            $re = CollectionDestinationModel::where('id',$id)->where('member_id',$member_id)->delete();
            if($re){
                return json(['code'=>200,'data'=>'','msg'=>'移除成功']);
            }else{
                return json(['code'=>3,'data'=>'','msg'=>'移除失败']);
            }
        }else{
            return json(['code'=>0,'data'=>'','msg'=>'非法请求']);
        }
    }
    //目的地列表
    public function get_destination(){
        if(request()->isPost()){
            if (!Session::get('member')) return json(['code' => 9, 'data' => '', 'msg' => '请登录']);
            $member_id = Session::get('member')['id'];
            $page = input('page')?input('page'):1;
            $start = $page == 1?0:($page-1)*4;
            //去重查询国家
            $count = CollectionDestinationModel::where('member_id',$member_id)->distinct(true)->field('country')->count();
            $country = Db::query('select distinct country from think_collection_destination where member_id = '.$member_id.' limit '.$start.',4');
            //目的地分国家查询旗下的目的地
            $data = $this->get_destination_counrty_group($country,$member_id);
            return json(['code'=>200,'data'=>$data,'count'=>$count,'msg'=>'ok']);
        }else{
            return json(['code'=>0,'data'=>'','msg'=>'非法请求']);
        }
    }
    //目的地分国家查询旗下的目的地
    protected function get_destination_counrty_group($country,$member_id){
        $data = [];
        if($country){
            foreach ($country as $k=>$v){
                //查询国家下的地址
                $data[$v['country']] = CollectionDestinationModel::where('member_id',$member_id)->where('country',$v['country'])->select();
            }
        }
        return $data;
    }

    //游记收藏
    public function c_travels(){
        if(request()->isPost()){
            if (!Session::get('member')) return json(['code' => 9, 'data' => '', 'msg' => '请登录']);
            $member_id = Session::get('member')['id'];
            $id = input('id');
            //查询该游记是否存在
            $travels = TravelsModel::get($id);
            if(!$travels || $travels['is_show'] != 1) return json(['code'=>1,'data'=>'','msg'=>'该游记不存在或没有通过审核']);
            //写入数据库
            $re = CollectionTravelsModel::create([
                'member_id'=>$member_id,
                'travels_id'=>$id
            ]);
            if($re){
                return json(['code'=>200,'data'=>'','msg'=>'收藏成功']);
            }else{
                return json(['code'=>1,'data'=>'','msg'=>'收藏失败']);
            }
        }else{
            return json(['code'=>0,'data'=>'','msg'=>'非法请求']);
        }
    }

    //收藏游记移除
    public function rm_travels(){
        if(request()->isPost()){
            if (!Session::get('member')) return json(['code' => 9, 'data' => '', 'msg' => '请登录']);
            $member_id = Session::get('member')['id'];
            $id = input('id');
            $re = CollectionTravelsModel::where('member_id',$member_id)->where('id',$id)->delete();
            if($re){
                return json(['code'=>200,'data'=>'','msg'=>'移除成功']);
            }else{
                return json(['code'=>3,'data'=>'','msg'=>'移除失败']);
            }
        }else{
            return json(['code'=>0,'data'=>'','msg'=>'非法请求']);
        }
    }
    //收藏游记列表
    public function c_travels_country_list(){
        if(request()->isPost()){
            if (!Session::get('member')) return json(['code' => 9, 'data' => '', 'msg' => '请登录']);
            $member_id = Session::get('member')['id'];
            //查询所有收藏的游记地址
           $address =  CollectionTravelsModel::alias('ct')->where('ct.member_id',$member_id)->join('think_travels t','t.id = ct.travels_id')->field('t.address')->select();
           $address_data =[];
           if($address){
               foreach ($address as $k=>$v){
                   $address_data[] = unserialize($v['address']);
               }
           }
           $data = $this->get_travels_country($address_data);
            return json(['code'=>200,'data'=>$data,'msg'=>'ok']);

        }else{
            return json(['code'=>0,'data'=>'','msg'=>'非法请求']);
        }
    }
    //二维数组转一维数组
    protected function merge_array($array){
        return call_user_func_array('array_merge',$array);
    }
    //收藏游记查询国家分组
    protected function get_travels_country($arr){
        $data = [];
        if($arr){
            //循环每一个游记
            foreach ($arr as $k=>$v){
                $country = [];
              //循环地址
                if($v){
                    foreach ($v as $k1=>$v1){
                        $country[$v1['country']] =1;
                    }
                }
                if($country){
                    foreach ($country as $k2=>$v2){
                        if(array_key_exists($k2,$data)){
                            $data[$k2]++;
                        }else{
                            $data[$k2] = 1;
                        }
                    }
                }
            }
        }
        return $data;
    }

    //收藏游记列表查询
    public function c_travels_list(){
        if(request()->isPost()){
            if (!Session::get('member')) return json(['code' => 9, 'data' => '', 'msg' => '请登录']);
            $member_id = Session::get('member')['id'];
            //接受参数
            $country = input('country');
            $page = input('page')?input('page'):1;
            //收藏游记的总数
            $count = TravelsModel::alias('t')->join('think_collection_travels ct','ct.travels_id = t.id')->where('ct.member_id',$member_id)->where('address','like',"%$country%")->count();
            if(!$country) return json(['code'=>1,'data'=>'','msg'=>'参数错误']);
            //分页查询
            $data = TravelsModel::alias('t')->join('think_collection_travels ct','ct.travels_id = t.id')->where('ct.member_id',$member_id)->where('address','like',"%$country%")->field('t.*')->page($page,10)->select();
            return json(['code'=>200,'data'=>$data,'count'=>$count,'msg'=>'ok']);
        }else{
            return json(['code'=>0,'data'=>'','msg'=>'非法请求']);
        }
    }

    //全部收藏列表
    public function c_all_list(){
        if(request()->isPost()){
            if (!Session::get('member')) return json(['code' => 9, 'data' => '', 'msg' => '请登录']);
            $member_id = Session::get('member')['id'];
            //获取这是第几页
            $page = input('page')?input('page'):1;
            //查询收藏的游记
            $c_travels = $this->f_travels($member_id,$page);
            //目的地
            $c_destination = $this->f_destination($member_id,$page);
            //景点
            $c_spot = $this->f_spot($member_id,$page);
            $data = [
                'c_travels'=>$c_travels['c_travels'],
                'c_destination'=>$c_destination['c_destination'],
                'c_spot'=>$c_spot['c_spot']
            ];
            //总页数去最大值
            $pages = [
                $c_travels['all_page'],
                $c_destination['all_page'],
                $c_spot['all_page'],
            ];
            $page_m = max($pages);
            return json(['code'=>200,'data'=>$data,'all_page'=>$page_m,'msg'=>'ok']);
        }else{
            return json(['code'=>0,'data'=>'','msg'=>'非法请求']);
        }
    }
    //全部收藏页面的游记
    protected function f_travels($member_id,$page){
        //收藏的游记总数
        $count = CollectionTravelsModel::where('member_id',$member_id)->count();
        $all_page = ($count % 2)==0?$count / 2:ceil($count / 2);
        $c_travels = CollectionTravelsModel::alias('ct')->where('ct.member_id',$member_id)->join('think_travels t','t.id = ct.travels_id')->order('ct.create_time desc')->page($page,2)->select();
        return [
            'all_page'=>$all_page,
            'c_travels'=>$c_travels
        ];
    }
    //全部收藏页面的目的地
    protected function f_destination($member_id,$page){
        //每页显示一个
        //目的地
        $start = $page == 1?0:($page-1)*4;
        //去重查询国家
        $count = Db::query('select distinct country from think_collection_destination where member_id = '.$member_id);
        $count = count($count);
        $country = Db::query('select distinct country from think_collection_destination where member_id = '.$member_id.' limit '.$start.',1');
        $c_destination = $this->get_destination_counrty_group($country,$member_id);
        return [
            'all_page'=>$count/1,
            'c_destination'=>$c_destination
        ];
    }
    //全部收藏页面景点  每页显示1个
    protected function f_spot($member_id,$page){
        //景点
        $count = CollectionSpotModel::where('member_id',$member_id)->count();
        $c_spot = CollectionSpotModel::where('member_id',$member_id)->order('create_time desc')->page($page,1)->select();
        return [
            'all_page' => $count/1,
            'c_spot'=>$c_spot
        ];
    }
    //返回收藏的数据
    public function get_cont_all(){
        if(request()->isPost()){
            if (!Session::get('member')) return json(['code' => 9, 'data' => '', 'msg' => '请登录']);
            $member_id = Session::get('member')['id'];
            //
            $data = array();
            $data['travels'] = $this->get_travels_all($member_id);
            $data['spot'] = $this->get_spot_all($member_id);
            $data['destination'] = $this->get_destination_all($member_id);
            return json(['code'=>200,'data'=>$data,'msg'=>'ok']);


        }else{
            return json(['code'=>0,'data'=>'','msg'=>'非法请求']);
        }
    }
    //获取所有收藏的游记
    protected function get_travels_all($member_id){
       $re =  CollectionTravelsModel::where($member_id)->field('travels_id')->select();
       $arr = array();
       if($re){
           foreach ($re as $k=>$v){
               $arr[] = $v['travels_id'];
           }
       }
       return $arr;
    }
    //获取收藏的景点
    protected function get_spot_all($member_id){
        $re =  CollectionSpotModel::where($member_id)->field('spot')->select();
        $arr = array();
        if($re){
            foreach ($re as $k=>$v){
                $arr[] = $v['spot'];
            }
        }
        return $arr;
    }
    //获取收藏地址
    protected function get_destination_all($member_id){
        $re = CollectionDestinationModel::where('member_id',$member_id)->select();
        $arr = array();
        if($re){
            foreach ($re as $k=>$v){
                $arr[]=[
                    'id'=>$v['id'],
                    'name'=>$v['city']
                ];
            }
        }
        return $arr;

    }
}